﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblImagesDAO
    {
        private DBService _db = null;
        public TblImagesDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public bool InsUpdImages(TblImages Image, int mode)
        {
            
                bool flag = false;
                try
                {
                    _db.ExeSP_NonQuery("sp_TblImages_InsUpd",
                        new string[] { "@IDImage", "@IDAlbum", "@ImageURL", "@DateUpload", "@GetCode", "@Display", "@Mode" },
                        new object[] { Image.IDImage, Image.IDAlbum, Image.ImageURL, Image.DateUpload, Image.GetCode, Image.Display, mode },
                        CommandType.StoredProcedure);
                    flag = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return flag;
            
        }

        public DataTable GetImageByIdAlbum(int IDAlbum)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblImages where IDAlbum = @IDAlbum",
                    new string[] { "@IDAlbum" },
                    new object[] { IDAlbum },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public TblImages GetImageByID(int IDImage)
        {
            TblImages img = new TblImages();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblImages where IDImage = @IDImage",
                    new string[] { "@IDImage" },
                    new object[] { IDImage },
                    CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    img.IDImage = IDImage;
                    img.IDAlbum = Convert.ToInt32(dr["IDAlbum"]);
                    img.ImageURL = dr["ImageURL"].ToString();
                    img.DateUpload = Convert.ToDateTime(dr["DateUpload"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return img;
        }
    }
}
